import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        int size=nums.length;
        List<List<Integer>> list;
        list = new ArrayList<>();
        for(int i=0;i<size-3;i++){
            if(i!=0 && nums[i]==nums[i-1]) continue;
            long num1=(long)(target-nums[i]);


            for(int j=i+1;j<size-2;j++){
                if(j!=i+1 && nums[j]==nums[j-1]) continue;
                long num2=(long)(num1-nums[j]);

                int left=j+1;
                int right=size-1;

                while(left<right){
                    if(left!=j+1 && nums[left]==nums[left-1])
                    {
                        left++;
                        continue;
                    }




                    if(nums[left]+nums[right]==num2){
                        List<Integer> list1=new ArrayList<>();
                        list1.add(nums[i]);
                        list1.add(nums[j]);
                        list1.add(nums[left]);
                        list1.add(nums[right]);

                        list.add(list1);
                        left++;
                        right--;


                    }else if(nums[left]+nums[right]>num2){
                        right--;
                    } else{
                        left++;
                    }
                }

            }



        }

        return list;



    }
}